package com.bill99.rpc.config.spring.schema;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.bill99.rpc.common.Constants;
import com.bill99.rpc.service.ServiceBean;

/**
 * @author sheng.zhang
 */
public class ServiceBeanDefinationParser extends
		AbstractRpcBeanDefinationParser {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	protected Class<?> getBeanClass(Element element) {
		return ServiceBean.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext,
			BeanDefinitionBuilder builder) {
		logger.info("to parse service configuration");
		String interfaceName = element.getAttribute(Constants.INTERFACE_KEY);
		if (interfaceName != null && interfaceName.trim().length() > 0) {
			builder.addPropertyValue("serviceInterface", interfaceName);
		}
		Object target = null;
		if (element.hasAttribute("ref")) {
			target = new RuntimeBeanReference(element.getAttribute("ref"));
		}
		if (target instanceof RuntimeBeanReference) {
			builder.addPropertyValue("service", target);
		}
		String protocol = element.getAttribute("protocol");
		if (protocol != null && protocol.trim().length() > 0) {
			builder.addPropertyValue("protocol", protocol);
		}
	}

}
